package file_demo1;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

// 判断文件夹的种类及个数
public class CNT {
    public static void main(String[] args) {
        File f = new File("D:\\biji");
        HashMap<String, Integer> hm = dg(f);
        System.out.println(hm);
    }

    public static HashMap<String, Integer> dg(File f) {
        HashMap<String, Integer> hm = new HashMap<>();
        if (f == null || !f.exists() || !f.isDirectory()) {
            return hm;
        }
        File[] files = f.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    String fileName = file.getName();
                    int lastIndex = fileName.lastIndexOf('.');
                    if (lastIndex != -1 && lastIndex < fileName.length() - 1) {
                        String endname = fileName.substring(lastIndex + 1);
                        hm.put(endname, hm.getOrDefault(endname, 0) + 1);
                    }
                } else if (file.isDirectory()) {
                    HashMap<String, Integer> son_map = dg(file);
                    Set<Map.Entry<String, Integer>> entrySet = son_map.entrySet();
                    for (Map.Entry<String, Integer> entry : entrySet) {
                        String ext = entry.getKey();
                        int count = entry.getValue();
                        hm.put(ext, hm.getOrDefault(ext, 0) + count);
                    }
                }
            }
        }
        return hm;
    }
}